//
//  TestStatusIndicatorVC.swift
//  StatusIndicatorExample
//
//  Created by kkkelicheng on 2022/1/15.
//

import UIKit


class RedView: UIView {
    deinit {
        print("red view deinit")
    }
}

class TestStatusIndicatorVC: UIViewController ,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    
    /*
     case none
     /// 正在loding
     case loading
     /// 没有数据
     case dataEmpty
     /// 网络异常
     case badNetwork
     /// 展示其他的错误
     case showError(String)
     /// 展示其他的view,设置自己的触发，可以不调用statusAction
     case showCustomView(StatusActionRequiredView)
     */
    
    var dateSource : [String] = ["无显示",
                                 "正在loding",
                                 "没有数据",
                                 "网络异常",
                                 "展示其他的错误",
                                 "展示其他的view"]
    
    lazy var testView : RedView = {
        let t = RedView.init(frame: .zero)
        t.backgroundColor = .red
        return t
    }()
    
    lazy var collectView: UICollectionView = {
        let c = UICollectionView.init(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout.init())
        let layout = UICollectionViewFlowLayout.init()
        c.delegate = self
        c.dataSource = self
        c.backgroundColor = .gray
        c.register(StatusTestItemCell.self, forCellWithReuseIdentifier: "StatusTestItemCell")
        layout.minimumInteritemSpacing = 10
        layout.minimumLineSpacing = 10
        return c
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        self.extendedLayoutIncludesOpaqueBars = false
        self.edgesForExtendedLayout = UIRectEdge(rawValue: 0)
        
        view.addSubview(testView)
        testView.translatesAutoresizingMaskIntoConstraints = false
        testView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        testView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        testView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        testView.topAnchor.constraint(equalTo: view.topAnchor, constant: 200).isActive = true
        testView.statusIndicator?.delegate = self
        
        view.addSubview(collectView)
        collectView.translatesAutoresizingMaskIntoConstraints = false
        collectView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        collectView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        collectView.bottomAnchor.constraint(equalTo: testView.topAnchor).isActive = true
        collectView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dateSource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let c = collectionView.dequeueReusableCell(withReuseIdentifier: "StatusTestItemCell", for: indexPath) as! StatusTestItemCell
        c.titleLabel.text = self.dateSource[indexPath.row]
        return c
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let content = self.dateSource[indexPath.row]
        let boundingSize = (content as NSString).boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: 50), options: [.usesLineFragmentOrigin], attributes: [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 13)], context: nil)
        let width = boundingSize.width
        return CGSize.init(width: width + 40, height: 30)
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        switch indexPath.row {
        case 0 :
            testView.statusIndicator?.status = .none
        case 1:
            testView.statusIndicator?.status = .loading
        case 2:
            testView.statusIndicator?.status = .dataEmpty
        case 3:
            testView.statusIndicator?.status = .badNetwork
        case 4:
            testView.statusIndicator?.status = .showError("其他错误")
        case 5:
            testView.statusIndicator?.status = .showCustomView(StatusIndicator_Customer())
        default:
            break
        }
    }
    
    deinit {
        print("TestStatusIndicator deinit")
    }
}

extension TestStatusIndicatorVC : StatusIndicatorDelegate{
    
    ///点击了status上的相关按钮
    func statusIndicatorClickAction(_ indicator : StatusIndicator){
        print("\(indicator.status)")
        switch indicator.status {
        case .badNetwork,.dataEmpty,.showError(_):
            statusIndicatorLoadData()
        case .showCustomView(let customView):
            print("\(customView)")
        default:
            break
        }
    }
    
    ///请求数据(为了规规范使用者)
    func statusIndicatorLoadData(){
        print("重新请求数据")
    }
}

class StatusTestItemCell: UICollectionViewCell {
    
    lazy var titleLabel: UILabel = {
        let l = UILabel.init(frame: .zero)
        l.font = UIFont.systemFont(ofSize: 13)
        l.textColor = .black
        return l
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.backgroundColor = .cyan
        contentView.addSubview(titleLabel)
        titleLabel.translatesAutoresizingMaskIntoConstraints = false
        titleLabel.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
        titleLabel.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
